package com.googlecode.allinclusive.fw.core.message;

import java.io.UnsupportedEncodingException;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.context.support.ResourceBundleMessageSource;

import com.googlecode.allinclusive.fw.core.config.GlobalConfig;

public class MessageUtils {

	/** ASCII转换*/
	public static final boolean NATIVE2ASCII = GlobalConfig.getBoolean("default.native2ascii", false);
	
	/** i18n资源文件*/
	public static final String[] I18N_RESOUERCES = GlobalConfig.getProperty("i18n.resources", "i18n/example.properties").split(",");
	
	/** log实例*/
	private static final Log log = LogFactory.getLog(MessageUtils.class);
	
	/** MessageSource实例*/
	private static final ResourceBundleMessageSource MESSAGE_SOURCE; 
	
	// 初始化MessageSource实例
	static {
		MESSAGE_SOURCE = new ResourceBundleMessageSource();
		MESSAGE_SOURCE.setBasenames(I18N_RESOUERCES);
	}
	
	/**
	 * 获得指定MessageKey的Message内容
	 * @param messageKey
	 * @return Message内容
	 */
	public static String getMessage(String messageKey) {
		return getMessage(messageKey, (Object[]) null);
	}
	
	/**
	 * 获得指定MessageKey的Message内容
	 * @param messageKey
	 * @return Message内容
	 */
	public static String getMessage(String messageKey, Object... args) {
		return getMessage(messageKey, args, (String) null);
	}
	
	/**
	 * 获得指定MessageKey的Message内容
	 * @param messageKey
	 * @return Message内容
	 */
	public static String getMessage(String messageKey, Object[] args, String defalutMessage) {
		return getMessage(messageKey, args, defalutMessage, Locale.getDefault());
	}
	
	/**
	 * 获得指定MessageKey的Message内容
	 * @param messageKey
	 * @return Message内容
	 */
	public static String getMessage(String messageKey, Object[] args, String defalutMessage, Locale locale) {
		String message = MESSAGE_SOURCE.getMessage(messageKey, args, defalutMessage, locale);
		if (message != null && !NATIVE2ASCII) {
			try {
				message = new String(message.getBytes("ISO-8859-1"), GlobalConfig.DEFAULT_ENCODING);
			} catch (UnsupportedEncodingException e) {
				log.warn(e);
			}
		}
		return message;
	}
	
	/**
	 * 获得指定MessageKey的Message内容
	 * @param messageKey
	 * @return Message内容
	 */
	public static String getMessage(MessageSourceResolvable resolvable) {
		return MESSAGE_SOURCE.getMessage(resolvable, Locale.getDefault());
	}
	
	/**
	 * 获得指定MessageKey的Message内容
	 * @param messageKey
	 * @return Message内容
	 */
	public static String getMessage(MessageSourceResolvable resolvable, Locale locale) {
		return MESSAGE_SOURCE.getMessage(resolvable, locale);
	}
	
	// --- example ----
	public static void main(String[] args) {
		System.out.println(MessageUtils.getMessage(MESSAGE.ERR_REQUIRED, new Object[] {FIELD.PASSWORD}));
		System.out.println(MessageUtils.getMessage(MESSAGE.ERR_MAXLENGTH, new Object[] {FIELD.PASSWORD, 12}));
		System.out.println(MessageUtils.getMessage(MESSAGE.ERR_MAXLENGTH, new Object[] {FIELD.PASSWORD, 12}, null, Locale.GERMAN));
		
	}
	interface FIELD {
		public static final MessageSourceResolvable USERNAME = new DefaultMessageSourceResolvable("comm.username");
		public static final MessageSourceResolvable PASSWORD = new DefaultMessageSourceResolvable("comm.password");
	}
	interface MESSAGE {
		public static final String ERR_REQUIRED = "error.required";
		public static final String ERR_MAXLENGTH = "error.maxlength";
	}
}
